<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 添加订单 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getOrders">
            <el-button slot="append" icon="el-icon-search" @click="getOrders"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单列表 -->
      <el-row>
        <el-table :data="orders" border stripe>
          <el-table-column type="index"></el-table-column>
          <el-table-column prop="order_number" label="订单编号"></el-table-column>
          <el-table-column prop="order_price" label="订单价格"></el-table-column>
          <el-table-column prop="order_pay" label="是否付款">
            <template slot-scope="scope">
              <el-tag
                :type="scope.row.order_pay === '0' ? 'danger' : 'success'"
              >{{scope.row.order_pay === '0' ? '未付款' : '已付款'}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="is_send" label="是否发货"></el-table-column>
          <el-table-column prop="create_time" label="下单时间">
            <template slot-scope="scope">
              <div>{{scope.row.create_time | dateFormat}}</div>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="showEditAddressDialog(scope.row.id)"
              ></el-button>
              <el-button
                type="success"
                icon="el-icon-location-outline"
                size="mini"
                @click="showLogisticDialog(scope.row.id)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <!-- 分页信息栏 -->
      <el-row>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="queryInfo.pagenum"
          :page-sizes="[5, 10, 15]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </el-row>
    </el-card>
    <!-- 修改地址弹出框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="editAddressDialogVisible"
      width="50%"
      @close="editAddressDialogClosed"
    >
      <!-- 表单列表 -->
      <el-form
        :model="editAddressForm"
        :rules="editAddressRules"
        ref="editAddressFormRef"
        label-width="80px"
      >
        <el-form-item label="省市区/县" prop="regions">
          <el-cascader
            :options="cityData"
            v-model="editAddressForm.regions"
            :props="editAddressProps"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="editAddressForm.address"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部按钮 -->
      <span slot="footer">
        <el-button @click="editAddressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editAddress">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 物流信息弹出框 -->
    <el-dialog title="物流进度" :visible.sync="logisticDialogVisible" width="50%">
      <el-timeline :reverse="true">
        <el-timeline-item
          v-for="(info, index) in logisticInfos"
          :key="index"
          :timestamp="info.time"
        >{{info.context}}</el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityData from "./citydata";

export default {
  data() {
    // 省份地区验证规则
    const checkRegion = (rule, value, callback) => {
      if (!value || (value.length !== undefined && value.length < 2)) {
        return callback(new Error("必须选择市区/县"));
      }
      callback();
    };
    return {
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      orders: [],
      cityData: cityData,
      // 修改地址相关
      editAddressDialogVisible: false,
      editAddressProps: {
        expandTrigger: "hover"
      },
      editAddressForm: {
        regions: [],
        address: ""
      },
      editAddressRules: {
        regions: [
          {
            validator: checkRegion,
            required: true,
            message: "请输入详细地址",
            trigger: "blur"
          }
        ],
        address: [
          {
            required: true,
            message: "请输入详细地址",
            trigger: "blur"
          }
        ]
      },
      // 物流信息相关
      logisticDialogVisible: false,
      logisticInfos: []
    };
  },
  methods: {
    async getOrders() {
      const { data: res } = await this.$http.get("/orders", {
        params: this.queryInfo
      });
      if (res.meta.status !== 200) {
        return this.$message.error("获取订单列表失败");
      }
      this.orders = res.data.goods;
      this.total = res.data.total;
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getOrders();
    },
    handleCurrentChange(current) {
      this.queryInfo.pagenum = current;
      this.getOrders();
    },
    showEditAddressDialog() {
      this.editAddressDialogVisible = true;
    },
    editAddressDialogClosed() {
      this.$refs.editAddressFormRef.resetFields();
    },
    editAddress() {
      this.editAddressDialogVisible = false;
    },
    showLogisticDialog() {
      this.getLogisticInfo();
      this.logisticDialogVisible = true;
    },
    async getLogisticInfo() {
      const { data: res } = await this.$http.get("kuaidi/804909574412544580");
      if (res.meta.status !== 200) {
        return this.$message.error("获取物流信息失败");
      }
      this.logisticInfos = res.data;
    }
  },
  created() {
    this.getOrders();
  }
};
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
